// pages/trainingVideo/index.js
import {
    trainingVideoIndex,
    trainingbanner,
    kbDocList
} from '../../../apis/index';

Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageNum:1,
        background: [],
        boxCurrent:0,
        oneLevelC:-1,
        twoLevelC:-1,
        threeLevelC:-1,
        showingLevel:0,
        videoType:1,
        showpop:false,
        levelList:[],
        levelList2:[],
        levelList3:[],
        showingList:[],
        videoList:[],
        loading:false,
        finshed:false,
        lastShow:1
    },
    toSearch(e){
        wx.navigateTo({
          url: '/pages/trainingVideo/searchRes/index?type=3',
        })
    },
    //最新 最热的切换
    choseType(e){
        // console.log(e)
        let videoType = e.target.dataset.videotype
        this.setData({
            pageNum:1,
            videoList:[],
            videoType : videoType
        })
        if(videoType == 1 ){
            this.orderByColumn = 'publishTime'
            this.GetkbDocList('publishTime',this.pid)
        }else{
            this.orderByColumn = 'clickNum'
            this.GetkbDocList('clickNum',this.pid)
        }
    },
    //这是banner的切换
    bindSwiperchange(e){
        this.setData({
            boxCurrent : e.detail.current
        })
    },
    //这是分类的popup弹出
    showOneFun(e){
        let type = e.target.dataset.stype || e.currentTarget.dataset.stype
        console.log(type)
        let showingList = []
        let showingLevel = 0
        this.lastShow = type
        switch(type){
            case '1' :
                showingList = this.data.levelList
                showingLevel = this.data.oneLevelC
            break
            case '2':
                showingList = this.data.levelList2
                showingLevel = this.data.twoLevelC
            break
            case '3':
                showingList = this.data.levelList3
                showingLevel = this.data.threeLevelC
            break
        }

        this.setData({
            showingList,
            showingLevel,
            lastShow:type,
            showpop : true,
        })
    },
    closePop(){
        this.setData({
            showpop : false
        })
    },
    //这是分类的选择
    choseOneFun(e,spe){ //一级
        let oneLevelC = -1
        if(e){
            oneLevelC =  e.target.dataset.index || e.currentTarget.dataset.index
        }else{
            oneLevelC =  spe
        }
        let levelList2 
        let levelList3
        if(oneLevelC == -1){
            levelList2 = []
            levelList3 = []
        }else{
            levelList2 = (this.data.levelList.length>0) &&  this.data.levelList[oneLevelC].children || []
            levelList3 = (levelList2.length>0) && levelList2[0].children || []
        }
      
        this.setData({
            pageNum:1,
            videoList:[],
            oneLevelC : oneLevelC,
            levelList2,
            levelList3:[],
            threeLevelC:-1,
            twoLevelC:-1
        })
        let pid = 0
        if(oneLevelC == -1){
            pid = ""
        }else if(this.data.twoLevelC == -1){
            pid = this.data.levelList[oneLevelC].projectId
        }else if(levelList3.length>0){
            pid = levelList3[0].projectId
        }else if(levelList2.length>0){
            pid = levelList2[0].projectId
        }else{
            pid = this.data.levelList[oneLevelC].projectId
        }
        this.pid = pid
        this.GetkbDocList(this.orderByColumn,pid)
    },
    choseTwoFun(e,spe){ //二级
        let twoLevelC = -1
        if(e){
            twoLevelC =  e.target.dataset.index || e.currentTarget.dataset.index
        }else{
            twoLevelC =  spe
        }
        
        let levelList3 
        if(twoLevelC == -1){
            levelList3 =  []
        }else{
            levelList3 =  this.data.levelList2[twoLevelC].children
        }
        // console.log(levelList3)
        this.setData({
            pageNum:1,
            videoList:[],
            twoLevelC : twoLevelC,
            threeLevelC:-1,
            levelList3
        })
        let pid = 0
        if(twoLevelC == -1){
            pid = this.data.levelList[this.data.oneLevelC].projectId
        }else if(this.data.threeLevelC == -1){
            pid = this.data.levelList2[twoLevelC].projectId
        }else if(levelList3.length>0){
            pid = levelList3[0].projectId
        }else{
            pid = this.data.levelList2[twoLevelC].projectId
        }
        this.pid = pid
        this.GetkbDocList(this.orderByColumn,pid)
    },
    choseThreeFun(e,spe){ //三级
        let threeLevelC = -1
        if(e){
            threeLevelC =  e.target.dataset.index || e.currentTarget.dataset.index
        }else{
            threeLevelC =  spe
        }
        console.log(threeLevelC)
        this.setData({
            threeLevelC : threeLevelC,
            pageNum:1,
            videoList:[]
        })
        let pid
        if(threeLevelC == -1){
            pid = this.data.levelList2[this.data.twoLevelC].projectId
        }else{
            pid = this.data.levelList3[threeLevelC].projectId
        }
        this.pid = pid
        this.GetkbDocList(this.orderByColumn,pid)
    },
    choseShowing(e){ //弹窗
        let showingLevel = e.target.dataset.index || e.currentTarget.dataset.index
        console.log(showingLevel)
        this.setData({
            showingLevel
        })
    },
    confirmFun(){ //确认弹窗
        switch(this.lastShow){
            case '1':
                this.choseOneFun(null,this.data.showingLevel)
            break
            case '2':
                this.choseTwoFun(null,this.data.showingLevel)
            break
            case '3':
                this.choseThreeFun(null,this.data.showingLevel)
            break
        }
        this.setData({
            showpop : false
        })
    },
     //获取一级二级三级那些
     GettrainingVideoIndex(){
        return new Promise((next)=>{
            trainingVideoIndex().then(res=>{
                let levelList = res.data.data
                let levelList2 = []
                let levelList3 =  []
                if(levelList[0] && levelList[0].children){
                    levelList2  = levelList[0].children
                }
                if(levelList2[0] && levelList2[0].children){
                    levelList3 = levelList2[0].children 
                }
                let pid = 0
                if(this.data.oneLevelC==-1){
                    pid = ''
                    levelList2 = levelList3 = []
                }else if (this.data.twoLevelC==-1){
                    pid = levelList[this.data.oneLevelC].projectId
                    levelList3 = []
                }else if (this.data.threeLevelC==-1){
                    pid = levelList2[this.data.twoLevelC].projectId
                }else if(levelList3.length>0){
                    pid = levelList3[0].projectId
                }else if(levelList2.length>0){
                    pid = levelList2[0].projectId
                }else{
                    pid = levelList[0].projectId
                }
                this.pid = pid
                this.setData({
                    levelList,
                    levelList2,
                    levelList3
                })
                next()
            })
        })
    },
    //获取文章列表
    GetkbDocList(orderByColumn='publishTime',projectId){
        let pageNum = this.data.pageNum
        wx.showLoading({
            title: '加载中...',
            mask: true
          });
        this.setData({
            loading:true
        })
        kbDocList({pageNum,pageSize:'10',orderByColumn,projectId}).then(res=>{
            wx.hideLoading()
            if(res.data.rows){
                let videoList = this.data.videoList.concat(res.data.rows)
                let ctr = false
                if(videoList.length>=res.data.total ){
                    ctr=true
                }
                this.setData({
                    pageNum:pageNum+=1,
                    finshed:ctr,
                    videoList
                })
            }
            this.setData({
                loading:false,
            })

        })
    },
    //获取banner
    trainingbannerFun(){
        trainingbanner(3).then(res=>{
            let background = res.data.rows || []
            this.setData({
                background
            })
        })
    },
    gotoZ(){
        wx.navigateTo({
          url: '/pages/trainingVideo/Information/index',
        })
    },
    gotoV(){
        wx.navigateTo({
          url: '/pages/trainingVideo/cooperationVideo/index',
        })
    },
    gotoZD(e){
        let vid = e.target.dataset.vid || e.currentTarget.dataset.vid
        wx.navigateTo({
          url: '/pages/trainingVideo/consultationDetail/index?vid=' + vid,
        })
    },


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.trainingbannerFun()
        this.GettrainingVideoIndex().then(()=>{
            this.GetkbDocList('publishTime',this.pid)
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if(!this.data.finshed && !this.data.loading){
            this.GetkbDocList(this.orderByColumn,this.pid)
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})
